#' @author edvard
#' @time 2017.02.01
#' 
# installing required packages
packages.name <- c("dplyr", "rvest", "dbscan", "devtools", "fpc", "timeDate", "mapproj")

devtools::install_github("dkahle/ggmap")
# Get rid of incompatible problem
devtools::install_github("hadley/ggplot2@v2.2.0")

devtools::install_github("kassambara/factoextra")
for(name in packages.name)
  if(!require(name)) install.packages(name)

devtools::install_github('badbye/baidumap')

rm(packages.name, name)

# Load required packages
# if you setup an .Rprofile then the package will be loaded automatically
library(dplyr)
library(httr)
library(rvest)
library(factoextra)
library(timeDate)

library(ggmap)
library(mapproj)
library(sp)
library(rgdal)
library(geosphere)
library(baidumap)
library(dbscan)
library(fpc)

library(arules)
library(arulesViz)

# Avoiding ultraedit
par(family='STXihei')

user_name <- "edvard"
user_data_path <- "users_data/edvard/"
getRawDataPath <- function(filename){
  return(paste(user_data_path, filename, sep = ""))
}

# Load raw data
raw.application <- read.csv(getRawDataPath("applications.csv"), sep=',', header = T, stringsAsFactors = F)
raw.location <- read.csv(getRawDataPath("locations.csv"),sep=',', header = T, stringsAsFactors = F)
raw.screen <- read.csv(getRawDataPath("screen.csv"),sep=',', header = T, stringsAsFactors = F)
raw.linear.accelerometer <- read.csv(getRawDataPath("linear_accelerometer.csv"),sep=',', header = T, stringsAsFactors = F)

# Load after preprocessing
raw.application <- read.csv("cache/applications.csv",sep=',', header = T, stringsAsFactors = F)
raw.location <- read.csv("cache/locations.csv",sep=',', header = T, stringsAsFactors = F)
raw.wifi <- read.csv("cache/wifi.csv",sep=',', header = T, stringsAsFactors = F)
raw.screen <- read.csv("cache/screen.csv",sep=',', header = T, stringsAsFactors = F)
raw.linear.accelerometer <- read.csv("cache/linear.accelerometer.csv",sep=',', header = T, stringsAsFactors = F)

rm(real.raw.linear.accelerometer)

# Load cache data
raw.linear.accelerometer <- read.csv("cache/cleaned.linear.accelerometer.csv",sep=',', header = T, stringsAsFactors = F)

# Load final format data
final.data <- read.csv(paste(user_data_path, "context_log.csv", sep = ""), sep = ",", header = T)

# Some useful function
#' Appending factor through fixed time period. Mainly used in grouping or splitting
#' @param data {data.frame} The input data must contain one column named timestamp
#' @param data {numeric} The length of period, unit is millisecond
#' @return data{data.frame}
debugonce("SegData2")
#' Multiple days
SegData2 <- function(data, period){
  levels <- data$date %>% levels()
  res <- data.frame()
  for(i in 1:length(levels)){
    res <- data %>% 
      filter(date == levels[i]) %>% 
      SegData(period) %>% 
      rbind(res)
  }
  return(res %>% arrange(timestamp))
}
#' Single day
SegData <- function(data, period){
  ts.min <- min(data$timestamp)
  datetime <- as.POSIXct(ts.min, origin="1970-01-01 00:00:00")
  date <- substr(datetime,0,10)
  ts.min <- as.numeric(as.POSIXct(date))

  # add a new column in data
  data <- data.frame(data, time_segment_no = 0)
  period.column.index <- dim(data)[2]
  
  for(i in 1:144){
    floor <- ts.min + (i-1)*period
    ceil <- ts.min + i*period
    data[which(data$timestamp >= floor & data$timestamp <= ceil), period.column.index] <- i  
  }
  
  return(data)
}

#' Obtaining the representative of linear accelerometer value in every specific time intervel
#' I use the mean value as respentative value at present, it can be change in the following experiment (or using normal distribution)
#' @param data {data.frame} 
#' @param data {numeric} 
#' @param processed.date {character} 
#' @return data{data.frame}
#' Multiple days
# debugonce("RepresentativeValueInLinearAccelerometer2")
RepresentativeValueInLinearAccelerometer2 <- function(data, period){
  levels <- data$date %>% levels()
  res <- data.frame()
  for(i in 1:length(levels)){
    res <- data %>% 
      filter(date == levels[i]) %>% 
      RepresentativeValueInLinearAccelerometer(period, levels[i]) %>% 
      rbind(res)
  }
  return(res)
}
#' Single day
RepresentativeValueInLinearAccelerometer <- function(data, period, processed.date){
  data <- data %>% 
    SegData(period) %>%  
    group_by(time_segment_no) %>% 
    summarise(mean_value = mean(magnitude))
  processed.data <- data.frame(data, date = processed.date)
  return(processed.data)
}

#' Attaching straightforward time information
#' @param x {data.frame} 
#' @return The list of datatime formatting as YYYY-mm-dd H:i:s
AppendTimeInfo <- function(data){
  tmp <- lapply(data$timestamp, DateTimeWeekdayFunc)
  tmp <- as.data.frame(tmp)
  tmp <- t(tmp)
  rownames(tmp) <- NULL
  colnames(tmp) <- c("date", "time", "weekday")
  data <- cbind(data, tmp)
  return(data)
}
DateTimeWeekdayFunc <- function(x){
  datetime <- as.POSIXct(x, origin="1970-01-01 00:00:00")
  date <- substr(datetime,0,10)
  time <- substr(datetime,12,19)
  weekday <- weekdays(datetime)
  if(weekday == "Monday" || weekday == "星期一"){
    weekday <- 1
  }
  if(weekday == "Tuesday" || weekday == "星期二"){
    weekday <- 2
  }
  if(weekday == "Wednesday" || weekday == "星期三"){
    weekday <- 3
  }
  if(weekday == "Thursday" || weekday == "星期四"){
    weekday <- 4
  }
  if(weekday == "Friday" || weekday == "星期五"){
    weekday <- 5
  }
  if(weekday == "Saturday" || weekday == "星期六"){
    weekday <- 6
  }
  if(weekday == "Sunday" || weekday == "星期天"){
    weekday <- 7
  }
  return(c(date, time, weekday))
}









